package com.alameda.service.user;

import com.alameda.api.user.UserOperateLogService;
import com.alameda.commonpent.commonback.api.CommonBackTemplate;
import com.alameda.mapper.user.UserOperateLogMapper;
import com.alameda.user.bo.UserOperateLogBO;
import com.alameda.user.dto.UserOperateLogDTO;
import com.alameda.user.enums.OperateResultEnum;
import com.alameda.user.pojo.UserOperateLogDO;
import com.alameda.user.vo.UserOperateLogVO;
import com.alameda.utils.ExcelUtils;
import com.alameda.utils.ObjectUtils;
import com.alameda.utils.PubUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 用户操作日志 业务逻辑层 具体实现
 *
 * @author Lv
 * @date 2025/01/09
 */
@Slf4j
@Service
public class UserOperateLogServiceImpl extends ServiceImpl<UserOperateLogMapper, UserOperateLogDO> implements UserOperateLogService {

    @Autowired
    private UserOperateLogMapper userOperateLogMapper;

    /**
     * 读取用户操作信息
     *
     * @param userOperateLogDTO 用户操作日志数据传递对象
     * @return
     */
    @Override
    public IPage<UserOperateLogVO> listUserOperateInfo(UserOperateLogDTO userOperateLogDTO) {
        IPage<UserOperateLogDO> iPage = new Page<>(userOperateLogDTO.getPageNum(), userOperateLogDTO.getPageSize());
        LambdaQueryWrapper<UserOperateLogDO> lambdaQueryWrapper = getLambdaQueryWrapper(userOperateLogDTO);
        IPage<UserOperateLogDO> pageData = userOperateLogMapper.selectPage(iPage, lambdaQueryWrapper);
        return convertData(pageData);
    }

    /**
     * 数据模型转换 UserOperateLogDO -> UserOperateLogVO
     *
     * @param pageData 用户操作信息
     * @return
     */
    private IPage<UserOperateLogVO> convertData(IPage<UserOperateLogDO> pageData) {
        IPage<UserOperateLogVO> convertPageData = ObjectUtils.convertPageData(pageData, UserOperateLogVO.class);
        convertPageData.getRecords().forEach(item -> {
            item.setOperateResultEnum(OperateResultEnum.getByKey(item.getOperateApiResult()));
            item.setOperateApiTimeFormat(formatTime(item.getOperateApiTime()));
        });
        return convertPageData;
    }

    /**
     * 格式化时间
     *
     * @param localDateTime 时间戳对象
     * @return
     */
    private String formatTime(LocalDateTime localDateTime) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return localDateTime.format(dateTimeFormatter);
    }

    /**
     * 导出excel
     *
     * @param userOperateLogDTO 用户操作日志数据传递对象
     */
    @Override
    public void exportExcel(UserOperateLogDTO userOperateLogDTO) {
        List<UserOperateLogBO> userOperateLogBOList = getExportData(userOperateLogDTO);
        ExcelUtils.export(userOperateLogBOList, UserOperateLogBO.class, "用户操作记录");
    }

    /**
     * 保存操作记录
     *
     * @param userOperateLogDTO 用户操作日志数据传递对象
     * @return
     */
    @Override
    public Boolean saveOperateLogInfo(UserOperateLogDTO userOperateLogDTO) {
        UserOperateLogDO userOperateLogDO = ObjectUtils.convert(userOperateLogDTO, UserOperateLogDO.class);
        return this.save(userOperateLogDO);
    }

    @SuppressWarnings("all")
    @Autowired
    private CommonBackTemplate commonBackTemplate;

    @Override
    public Boolean deleteOperateLog(String sql) {
        Integer count = commonBackTemplate.executeDelete(sql);
        log.info("删除数据量 :{}", count);
        return Boolean.TRUE;
    }

    /**
     * 获取导出数据
     *
     * @param userOperateLogDTO 用户操作日志数据传递对象
     * @return
     */
    private List<UserOperateLogBO> getExportData(UserOperateLogDTO userOperateLogDTO) {
        if (PubUtils.isNotEmpty(userOperateLogDTO.getIdList())) {
            List<UserOperateLogDO> userOperateLogDOList = this.findByIdInOrderByCreateTimeDesc(userOperateLogDTO.getIdList());
            return ObjectUtils.convertList(userOperateLogDOList, UserOperateLogBO.class);
        } else {
            IPage<UserOperateLogVO> pageData = listUserOperateInfo(userOperateLogDTO);
            return ObjectUtils.convertList(pageData.getRecords(), UserOperateLogBO.class);
        }
    }

    /**
     * 获取构造查询条件
     *
     * @param userOperateLogDTO 用户操作日志数据传递对象
     * @return
     */
    private LambdaQueryWrapper<UserOperateLogDO> getLambdaQueryWrapper(UserOperateLogDTO userOperateLogDTO) {
        return Wrappers.lambdaQuery(UserOperateLogDO.class)
                .like(PubUtils.isNotEmpty(userOperateLogDTO.getUserName()), UserOperateLogDO::getUserName, userOperateLogDTO.getUserName())
                .eq(PubUtils.isNotNull(userOperateLogDTO.getUserType()), UserOperateLogDO::getUserType, userOperateLogDTO.getUserType())
                .like(PubUtils.isNotEmpty(userOperateLogDTO.getOperateApiName()), UserOperateLogDO::getOperateApiName, userOperateLogDTO.getOperateApiName())
                .eq(PubUtils.isNotEmpty(userOperateLogDTO.getOperateApiResult()), UserOperateLogDO::getOperateApiResult, userOperateLogDTO.getOperateApiResult())
                .like(PubUtils.isNotEmpty(userOperateLogDTO.getOperateApiTraceId()), UserOperateLogDO::getOperateApiTraceId, userOperateLogDTO.getOperateApiTraceId())
                .orderByDesc(UserOperateLogDO::getCreateTime);
    }
}